import Vue from 'vue'
import VueRouter from 'vue-router'
import store from "../store/index"

Vue.use(VueRouter)  

//验证某个路由的权限
function checkRoute(path, next) {
  if (store.state.user.menus_url.includes(path)) {
    next()
  } else {
    next("/login")
  }
}

export const routersPath = [
     {path:"menu",component:()=>import ("../pages/menu/menu.vue"),meta:{title:"菜单管理"},beforeEnter(to,from,next){checkRoute("/menu",next)}},
    {path:"role",component:()=>import ("../pages/role/role.vue"),meta:{title:"角色管理"},beforeEnter(to,from,next){checkRoute("/role",next)}},
    {path:"manage",component:()=>import ("../pages/manage/manage.vue"),meta:{title:"管理员管理"},beforeEnter(to,from,next){checkRoute("/manage",next)}},
    {path:"cate",component:()=>import ("../pages/cate/cate.vue"),meta:{title:"商品分类"},beforeEnter(to,from,next){checkRoute("/cate",next)}},
    {path:"specs",component:()=>import ("../pages/specs/specs.vue"),meta:{title:"商品规格"},beforeEnter(to,from,next){checkRoute("/specs",next)}},
    {path:"goods",component:()=>import ("../pages/goods/goods.vue"),meta:{title:"商品管理"},beforeEnter(to,from,next){checkRoute("/goods",next)}},
    {path:"vip",component:()=>import ("../pages/vip/vip.vue"),meta:{title:"会员管理"},beforeEnter(to,from,next){checkRoute("/vip",next)}},
    {path:"banner",component:()=>import ("../pages/banner/banner.vue"),meta:{title:"轮播图管理"},beforeEnter(to,from,next){checkRoute("/banner",next)}},
    {path:"seckill",component:()=>import ("../pages/seckill/seckill.vue"),meta:{title:"秒杀活动"},beforeEnter(to,from,next){checkRoute("/seckill",next)}},
]

const routes = [
  {path:"/login",component:()=>import ("../pages/login/login.vue"),meta:{title:"登录"}},
  {path:"/",component:()=>import ("../pages/index/index.vue"),meta:{title:"index"},
  children:[
    {path:"",component:()=>import ("../pages/home/home.vue"),meta:{title:"主页"}},
    ...routersPath
  ]
},
]

const router = new VueRouter({
  routes
})

export default router
